package com.example.gitstudy.config.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.example.gitstudy.config.LoginUser;
import com.example.gitstudy.config.entity.User;
import com.example.gitstudy.config.mapper.MenuMapper;
import com.example.gitstudy.config.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author hwj
 * @date 2023/1/12 10:15
 * @描述；
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private MenuMapper menuMapper;


    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,userName);
        User user = userMapper.selectOne(queryWrapper);
        if(Objects.isNull(user)){
            throw new RuntimeException("用户名或密码错误");
        }

        //TODO 根据用户查询权限信息 添加到LoginUser中

//        List<String> list = new ArrayList<>(Arrays.asList("test"));

//        List<String> strings = menuMapper.selectPermsByUserId(user.getId());

        return new LoginUser(user);
    }
}
